<?php

namespace app\admin\validate;

use think\facade\Request;
use think\Validate;

class User extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'username|用户名' => 'require',
        'password|密码' => 'require',
        'nickname|昵称' => 'require',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [];

    /**
     * 用户登录场景
     * @return User
     */
    public function sceneLogin()
    {
        return $this->append('captcha|验证码', 'require|captcha')
            ->remove('nickname');
    }

    /**
     * 添加用户时验证
     * @return User
     */
    public function sceneAdd()
    {
        return $this->append('password_confirm|密码', 'require|confirm:password')
            ->append('password', 'confirm:password_confirm');
    }

    public function sceneEdit()
    {
        if ((Request::has('password') && Request::get('password')) || (Request::has('password_confirm') && Request::get('password'))) {
            return $this->append('password_confirm|密码', 'require|confirm:password')
                ->append('password', 'confirm:password_confirm');
        } else {
            return $this->remove('password');
        }
    }

}
